﻿using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Windows.Forms;
using BGShell.PowerShell.Host;

namespace BGShell.PowerShell.Runspaces
{
    internal sealed class KeyMappingDictionary : IDictionary<Keys, Object>
    {
        private readonly IRunspaceHostUI _hostUi;
        private readonly Dictionary<Keys, Object> _values = new Dictionary<Keys, Object>();

        public KeyMappingDictionary(IRunspaceHostUI hostUI)
        {
            _hostUi = hostUI;
        }

        public void Add(Keys key, Object value)
        {
            _values.Add(key, value);
            _hostUi.RegisterHookKeys(new Keys[] { key });
        }

        public void Clear()
        {
            _hostUi.UnregisterHookKeys(_values.Keys);
            _values.Clear();
        }

        public bool ContainsKey(Keys key)
        {
            return _values.ContainsKey(key);
        }

        public bool Remove(Keys key)
        {
            if (_values.Remove(key))
            {
                _hostUi.UnregisterHookKeys(new Keys[] { key });
                return true;
            }

            return false;
        }

        public bool TryGetValue(Keys key, out object value)
        {
            return _values.TryGetValue(key, out value);
        }

        public Object this[Keys key]
        {
            get
            {
                return _values[key];
            }
            set
            {
                _values[key] = value;
                _hostUi.RegisterHookKeys(new Keys[] { key });
            }
        }

        public Int32 Count
        {
            get { return _values.Count; }
        }

        public ICollection<Keys> Keys
        {
            get { return _values.Keys; }
        }

        public ICollection<Object> Values
        {
            get { return _values.Values; }
        }

        #region ICollection<KeyValuePair<Keys,object>> Members

        void ICollection<KeyValuePair<Keys, Object>>.Add(KeyValuePair<Keys, Object> item)
        {
            ((ICollection<KeyValuePair<Keys, Object>>)(_values)).Add(item);
        }

        bool ICollection<KeyValuePair<Keys, Object>>.Contains(KeyValuePair<Keys, Object> item)
        {
            return ((ICollection<KeyValuePair<Keys, Object>>)(_values)).Contains(item);
        }

        void ICollection<KeyValuePair<Keys, Object>>.CopyTo(KeyValuePair<Keys, Object>[] array, int arrayIndex)
        {
            ((ICollection<KeyValuePair<Keys, Object>>)(_values)).CopyTo(array, arrayIndex);
        }

        bool ICollection<KeyValuePair<Keys, Object>>.IsReadOnly
        {
            get { return ((ICollection<KeyValuePair<Keys, Object>>)(_values)).IsReadOnly; }
        }

        bool ICollection<KeyValuePair<Keys, Object>>.Remove(KeyValuePair<Keys, Object> item)
        {
            return ((ICollection<KeyValuePair<Keys, Object>>)(_values)).Remove(item);
        }

        #endregion

        #region IEnumerable<KeyValuePair<Keys,object>> Members

        IEnumerator<KeyValuePair<Keys, Object>> IEnumerable<KeyValuePair<Keys, Object>>.GetEnumerator()
        {
            return _values.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _values.GetEnumerator();
        }

        #endregion

        internal BGShellPipeline GetPipeline(Keys key)
        {
            object info;

            if (_values.TryGetValue(key, out info))
            {
                var scriptBlock = (info as ScriptBlock);

                if (scriptBlock != null)
                {
                    return new BGShellPipeline(scriptBlock);
                }
            }

            return null;
        }
    }
}
